package com.ease.web.struts2;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.ease.common.util.Detect;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author nathanleewei
 * 
 */
public class ServletAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7240721520254922139L;

	public static final String FAILURE = "failure";

	// public static final String AJAX_REQUEST_ENCODING = "UTF-8";

	public String encode(String value) {
		if (Detect.notEmpty(value)) {
			try {
				return URLEncoder.encode(value, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// e.printStackTrace();
				// log
			}
		}
		return null;
	}

	protected ActionContext getActionContext() {
		return ActionContext.getContext();
	}

	protected String getRemoteUser() {
		return ServletActionContext.getRequest().getRemoteUser();
	}

	protected String getHeader(String key) {
		return ServletActionContext.getRequest().getHeader(key);
	}

	protected Map<String, Object> getParameterMap() {
		return getActionContext().getParameters();
	}

	protected Map<String, Object> getSessionMap() {
		return getActionContext().getSession();
	}

	protected String getParameter(String key) {
		String[] values = getParameterValues(key);
		if ((null != values) && (values.length > 0)) {
			return values[0];
		}
		return null;
	}

	protected String[] getParameterValues(String key) {
		Object object = getParameterMap().get(key);

		if (null != object) {
			return (String[]) object;
		}

		return null;
	}

	protected long getLongParameter(String key) {
		String[] values = getParameterValues(key);
		if ((null != values) && (values.length > 0)) {
			return Long.parseLong(values[0]);
		}
		return -1;
	}

	private HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	protected String getRemoteHost() {
		return getRequest().getRemoteHost();
	}

	protected String getRemoteAddr() {
		return getRequest().getRemoteAddr();
	}

	protected Object getRequestAttribute(String key) {
		return getRequest().getAttribute(key);
	}

	protected void setRequestAttribute(String key, Object object) {
		getRequest().setAttribute(key, object);
	}

	protected void addCookie(String key, String value) {
		Cookie cookie = new Cookie(key, value);
		getResponse().addCookie(cookie);
	}

	protected Cookie[] getCookies() {
		return getRequest().getCookies();
	}

	protected void removeRequestAttribute(String key) {
		getRequest().removeAttribute(key);
	}

	public String getContextPath() {
		return getRequest().getContextPath();
	}

	private HttpSession getSession() {
		return getRequest().getSession();
	}

	protected void setSessionAttribute(String key, Object object) {
		getSession().setAttribute(key, object);
	}

	protected void removeSessionAttribute(String key) {
		getSession().removeAttribute(key);
	}

	protected Object getSessionAttribute(String key) {
		return getSession().getAttribute(key);
	}

	protected String encodeRedirectURL(String url) {
		return this.getResponse().encodeRedirectURL(url);
	}

	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	protected ServletContext getServletContext() {
		return getSession().getServletContext();
	}

	protected void setServletContextAttribute(String key, Object object) {
		getServletContext().setAttribute(key, object);
	}

	protected Object getServletContextAttribute(String key) {
		return getServletContext().getAttribute(key);
	}

	protected void removeServletContextAttribute(String key) {
		getServletContext().removeAttribute(key);
	}

	protected String getRealPath() {
		return getServletContext().getRealPath("/");
	}

	/** return StringBuffer: "http://127.0.0.1:8080/module/action?parameters" */
	protected StringBuffer getRequestFullUrl() {
		return ServletActionContext.getRequest().getRequestURL();
	}

	/** return String: "/action?parameters" */
	public String getRequestServiceUrl() {
		return RequestUtil.getRequestUrl();
	}

	/** return String: "/action?parameters" */
	protected String getRequestString() throws Exception {
		return RequestUtil.getRequestString();
	}

	public URL getURL(String relativePath) throws Exception {

		HttpServletRequest request = getRequest();

		String serverName = request.getServerName();
		boolean isSecure = request.isSecure();
		int port = request.getServerPort();
		String protocol = null;

		if (isSecure == false) {
			protocol = "http";
			if (port == 80) {
				port = -1;
			}
		} else {
			protocol = "https";
			if (port == 443) {
				port = -1;
			}
		}
		return new URL(protocol, serverName, port, relativePath);

	}

}